package com.podium.comment.validation.validator;

import com.podium.comment.utils.StringUtils;
import com.podium.comment.validation.annotation.Phone;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Pattern;

/**
 * @author 邵文泽
 * @description 电话验证器
 * @date 2025/5/14 10:29
 */
public class PhoneValidator implements ConstraintValidator<Phone,String> {

    private static final Pattern PHONE = Pattern.compile("^1[3456789]\\d{9}$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (!StringUtils.hasLength(value)){
            return true;
        }
        return PHONE.matcher(value).matches();
    }
}
